import { Injectable } from '@angular/core';
import { ToastController, AlertController } from 'ionic-angular';

@Injectable()
export class NoticeService {
	static TOAST_POS_BOTTOM: string = 'bottom';
	static TOAST_POS_MIDDLE: string = 'middle';

	constructor(private toastCtrl: ToastController, private alertCtrl: AlertController) { }

	// 显示 toast提示
	showToast(message: string, position: string = NoticeService.TOAST_POS_BOTTOM) {

		let toast = this.toastCtrl.create({ message: message, cssClass: "toastCtrl", duration: 3000, position: position, closeButtonText: 'Ok' });
		toast.onDidDismiss(this.dismissHandler);
		toast.present();

		return toast;
	}
	private dismissHandler() {
		console.info('Toast onDidDismiss()');
	}
	/**
   * 底部上弹提示 提示
   * @param code 1、提示 2、错误
   * @param msg 提示字符
   */
	showNoticeByToast(code: Number, msg: string) {
		let m = '';

		if (msg && msg.length > 0) {
			if (msg.charAt(msg.length - 1) == '!' || msg.charAt(msg.length - 1) == '！') {
				msg = msg.substr(0, msg.length - 1);
			}
		}

		if (code == 1) {
			m = '提示：' + msg + '！';
		} else if (code == 2) {
			m = '错误' + '：' + msg + '！';
		}
		else if (code == 3) {

			m = '错误' + '：' + msg + '！';
		}

		return this.showToast(m);
	}




	/**
	 * 弹出窗 填写内容
	 * @param title 
	 * @param message 
	 * @param success 
	 */
	presentPrompt(title, message, success=null) {
		let alert = this.alertCtrl.create({
			title: title,
			message: message,
			inputs: [
				{
					name: 'msg',
					placeholder: message
				}
			],
			buttons: [
				{
					text: '取消',
					role: 'cancel',
					handler: data => {

					}
				},
				{
					text: '确认',
					handler: data => {
						success(data);
					}
				}
			]
		});
		alert.present();
	}
	/**
	 * 单选Alert
	 * @param title 
	 * @param options 
	 * @param success 
	 */
	roleAlert(title, options, success = null){
		let alert = 	this.alertCtrl.create({
			title: title,
			// message: message,
		});
		for (let index = 0; index < options.length; index++) {
			alert.addInput(options[index]);
		}
		alert.addButton('取消');
		alert.addButton({
			text: '确定',
			handler: data => {
				// console.log('Radio data:', data);
				// this.testRadioOpen = false;
				// this.testRadioResult = data;
				success(data);
			}
		});
		alert.present();
	
		
	}
	/**
	 * 提示框
	 * @param title 
	 * @param subTitle 
	 */
	presentAlert(title, subTitle) {
		let alert = this.alertCtrl.create({
			title: title,
			subTitle: subTitle,
			buttons: ['确认']
		});
		alert.present();
	}
	/**
	 * 确认框
	 */
	presentConfirm(title, subTitle,isok) {
		let alert = this.alertCtrl.create({
			title: title,
			subTitle: subTitle,
			buttons: [
				{
					text: '取消',
					role: 'cancel',
					handler: () => {
						console.log('Cancel clicked');
					}
				},
				{
					text: '确认',
					handler: () => {
						console.log('Buy clicked');
						isok();
					}
				}
			]
		});
		alert.present();
	}
	/**
	 * 
	 */
	showToastWithCloseButton(msg, succes = null) {
		let toast = this.toastCtrl.create({
			message: msg,
			showCloseButton: true,
			closeButtonText: "确定",
			dismissOnPageChange: false,
			position: NoticeService.TOAST_POS_BOTTOM
		});

		toast.onDidDismiss((data, role) => {
			succes&&succes();
		});
		toast.present();
		return toast;
	}




}
